package com.kiwilss.aopdemo.ui.lifecycler

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import com.kiwilss.aopdemo.R
import com.kiwilss.aopdemo.ui.WorkUtil
import com.kiwilss.aopdemo.utils.LogUtils

/**
 * @author : Administrator
 * e-mail : kiwilss@163.com
 * time   : 2021/02/01
 *  desc  : https://blog.csdn.net/Alexwll/article/details/80638905
 *  https://blog.csdn.net/huangliniqng/article/details/106218260
 */
//自己实现
//class LifeCyclerActivity: AppCompatActivity(R.layout.activity_lifecycler) ,LifecycleOwner{
//
//    lateinit var lifecycleRegistry: LifecycleRegistry
//
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//
//        lifecycleRegistry = LifecycleRegistry(this)
//        //实例化MyObserver
//        var myObserver = MyObserver(lifecycleRegistry,object :Callback{
//            override fun update(string: String) {
//                LogUtils.e(string,"update:")
//            }
//
//        })
//
//        lifecycleRegistry.addObserver(myObserver)
//        lifecycleRegistry.currentState = Lifecycle.State.CREATED
//    }
//
//    override fun onResume() {
//        super.onResume()
//        lifecycleRegistry.currentState = Lifecycle.State.RESUMED
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
//    }
//
//}

//activity中已经实现LifecycleOwner
class LifeCyclerActivity: AppCompatActivity(R.layout.activity_lifecycler){


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //实例化MyObserver
        var myObserver = MyObserver(lifecycle,object :Callback{
            override fun update(string: String) {
                LogUtils.e(string,"update:")
                //lifecycle.currentState当前声明周期状态
            }

        })
        lifecycle.addObserver(myObserver)


    }



}